global class AT_ProcessAllDLogs implements Database.Batchable<Sobject>
{
	global string query;

	global AT_ProcessAllDLogs(string q)
	{
		query = q;
	}
	
	global AT_ProcessAllDLogs()
	{
		query = 'Select ID, Status__c, Response_Text__c, Request_Text__c From AT_Debug_Log__c where Status__c=\'Success\'';
		//query = 'Select RecordType.Name, RecordTypeId, Id, Latest_Fund_Amount__c, Latest_Fund_Amount_Rank__c, Latest_Fund_Amount_Rank_X_Wtg__c From Account where RecordType.DeveloperName=\'Investor\' and Latest_Fund_Amount__c>0 order by Latest_Fund_Amount__c';
	}

	global static String checkResStatus(String inXML)
	{
		String strRes = 'Failure';
		
		TG_XmlDom dom = new TG_XmlDom(inXML);
    	TG_XmlNode root = dom.root;
    	TG_XmlNode operationXN = root.getElementByTagName('operation');
    	TG_XmlNode resultXN = operationXN.getElementByTagName('result');
    	String sTransactionStatus = resultXN.getElementByTagName('status').textContent;
    	if(sTransactionStatus!=null && sTransactionStatus!='failure')
    	{
    		strRes = 'Success';
    	}
		
		return strRes;
	}
	
	
	global Database.QueryLocator start(Database.BatchableContext BC)
	{
		return Database.getQueryLocator(query);
	}
	
	global void execute(Database.BatchableContext BC, List<sObject> scope)
	{
		
		List<AT_Debug_Log__c> listItem2Upd = new List<AT_Debug_Log__c>();

		for(sObject item:scope)
		{
			AT_Debug_Log__c tmpItem = (AT_Debug_Log__c)item;
			if(checkResStatus(tmpItem.Response_Text__c)=='Failure')
			{
				tmpItem.Status__c = 'Failure';
				listItem2Upd.add(tmpItem);
			}
		}
		if(listItem2Upd.size()>0) update listItem2Upd;
	}
	
	global void finish(Database.BatchableContext BC)
	{
		
	}	
	
	public static testMethod void testIt()
	{

		AT_Debug_Log__c DLog = new AT_Debug_Log__c();
		DLog.Request_Text__c = '';
		DLog.Response_Text__c = '<?xml version="1.0" encoding="UTF-8"?>'
			+ '<response>'
			+ '<control>'
			+ '<status>success</status>'
			+ '<senderid>intego</senderid>'
			+ '<controlid>ControlIdHere</controlid>'
			+ '<uniqueid>false</uniqueid>'
			+ '<dtdversion>2.1</dtdversion>'
			+ '</control>'
			+ '<operation>'
			+ '<authentication>'
			+ '<status>success</status>'
			+ '<userid>ps_config</userid>'
			+ '<companyid>NHI-TEST</companyid>'
			+ '</authentication>'
			+ '<result>'
			+ '<status>failure</status>'
			+ '<function>create_sotransaction</function>'
			+ '<controlid>414EB0F1-3CD4-4aa3-917F-3A15BF326F13</controlid>'
			+ '<key>FastSpring Sales Order-SO2372</key>'
			+ '</result>'
			+ '</operation>'
			+ '</response>';
		DLog.Status__c = 'Success';
		DLog.Transaction_Key__c = null;
		insert DLog;

		List<sObject> list2Test = new List<sObject>();
		list2Test.add(DLog);
		
		System.Test.startTest();
		
		AT_ProcessAllDLogs controller = new AT_ProcessAllDLogs();
		controller = new AT_ProcessAllDLogs('Select ID, Status__c, Response_Text__c, Request_Text__c From AT_Debug_Log__c where Status__c=\'Success\' LIMIT 1');
		
		Database.BatchableContext BC2 = null;
		controller.start(BC2);
		controller.execute(BC2,list2Test);
		controller.finish(BC2);

		System.Test.stopTest();

	}		

}